package study.junit.matcher;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

public class DateMatcher extends BaseMatcher<Date> {

	private int year;
	private int month;
	private int date;

	private Object actual;

	public static Matcher<Date> dateOf(int year, int month, int date){
		DateMatcher instance = new DateMatcher();
		instance.year = year;
		instance.month = month;
		instance.date = date;
		return instance;
	}

	public boolean matches(Object item) {
		this.actual = item;

		if(item == null) return false;
		if(!(item instanceof Date)) return false;

		Date actualDate = (Date)item;
		Calendar cal = Calendar.getInstance();
		cal.setTime(actualDate);
		if(cal.get(Calendar.YEAR) != year) return false;
		if(cal.get(Calendar.MONTH) + 1 != month) return false;
		if(cal.get(Calendar.DATE) != date) return false;
		return true;
	}

	public void describeTo(Description description) {
		description.appendValue(String.format("%d/%02d/%02d", year, month, date));
		description.appendText("but actual is ");
		if(actual != null){
			description.appendValue(new SimpleDateFormat("yyyy/MM/dd").format((Date) actual));
		}else{
			description.appendValue(null);
		}
	}

}
